package com.pi.nbcenter.device.controller.iot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pi.base.dto.result.AppResult;
import com.pi.base.dto.result.ServiceResult;
import com.pi.iot.repository.constants.PiInsEnum;
import com.pi.nbcenter.device.bean.dto.ins.InsFingerParam;
import com.pi.nbcenter.device.service.pi.external.PiInsService;

@RequestMapping("/userfinger")
@RestController
public class UserFingerController {
  
  @Autowired
  private PiInsService piInsService;

  @PostMapping("/rise")
  public AppResult localFingerEventRise(String imei, Long userId, Long sourceId) 
      throws Exception{
    InsFingerParam insParam = new InsFingerParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setUserId(userId);
    insParam.setInsEnum(PiInsEnum.FINGER_ADD_RISE);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
  
  @PostMapping("/delete")
  public AppResult deleteUserFinger(String imei, Long userId, Short fingerId, Long sourceId) 
      throws Exception{
    InsFingerParam insParam = new InsFingerParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setUserId(userId);
    insParam.setFingerId(fingerId);
    insParam.setInsEnum(PiInsEnum.FINGER_DEL);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
  
  @PostMapping("/clear")
  public AppResult clearUserFinger(String imei, Long userId, Long sourceId) 
      throws Exception{
    InsFingerParam insParam = new InsFingerParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setUserId(userId);
    insParam.setInsEnum(PiInsEnum.FINGER_CLEAR);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
}
